#!coding=utf-8
from django.template.context import RequestContext
from django.shortcuts import render_to_response
from nachuan.models import Company_admin, Company_fee
from django.core.exceptions import ObjectDoesNotExist
from django.http.response import HttpResponse
from weixin.models import User
from django.db.models import Q

def addcompany(request):
    CompanyName=request.POST.get("CompanyName");
    CompanyFR=request.POST.get("CompanyFR");
    ContactPhone=request.POST.get("ContactPhone");
    Certid=request.POST.get("Certid");
    Sender=request.POST.get("Sender");
    Address=request.POST.get("Address");
    openid=request.POST.get("openid");
    try:
        user=User.objects.get(openid=openid)
    except ObjectDoesNotExist:
        User(openid=openid).save()
    try:
        company=Company_admin.objects.get(wx_id=openid,companyname=CompanyName)
        company.adminname=CompanyFR
        company.city=Sender
        company.adress=Address
        company.adminphone=ContactPhone
        company.admincertid=Certid
        company.save();
    except ObjectDoesNotExist:
        tcom=Company_admin(wx_id=openid,companyname=CompanyName,adminname=CompanyFR,city=Sender,adress=Address,adminphone=ContactPhone,admincertid=Certid)
        tcom.save();
    return HttpResponse("true");

def viewcompany(request):
    openid=request.GET.get("openid");
    try:
        tcom_ad=Company_admin.objects.filter(wx_id=openid)
        params={'company':tcom_ad[0]}
    except ObjectDoesNotExist:
        params={}
    template='addcompinfo.html'
    return render_to_response(template,params,context_instance=RequestContext(request))


def feelist(request):
    openid=request.GET.get("openid");
    id=[]
    comadmin=Company_admin.objects.filter(wx_id=openid)
    
    for tmpcomadmin in comadmin:
        id.append(tmpcomadmin.id);
    tmpcomfee=Company_fee.objects.filter(company_id__in=id)
    
    params={'rescomadmin':tmpcomfee}
    template='feelist.html'
    
    return render_to_response(template,params,context_instance=RequestContext(request))




    